//
//  QuizSubmitVC.m
//  SubmitVCDemo
//
//  Created by Tianlong on 2018/1/23.
//  Copyright © 2018年 Tianlong. All rights reserved.
//

#import "QuizSubmitVC.h"
#import "QuizSubmitTopView.h"
#import "QuizAnalyView.h"
#import "QuizTalkView.h"
#import "QuizScoreView.h"
#import "QuizSubmitListCell.h"
#import "QuizHeadView.h"

@interface QuizSubmitVC ()<UITableViewDelegate,UITableViewDataSource,QuizAnalyViewDelegate>
@property (weak, nonatomic) UITableView *tableView;
@property (assign, nonatomic) QuizSubmitType type;
@property (strong, nonatomic) NSMutableArray *dataM;
@property (strong, nonatomic) QuizHeadView *headV;
@end

@implementation QuizSubmitVC

+ (instancetype)quizSubmitVCWithType:(QuizSubmitType)type{
    QuizSubmitVC *submitVC = [[QuizSubmitVC alloc] init];
    submitVC.type = type;
    return submitVC;
}

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    
    for (int i = 0; i < 20; i++) {
        QuizSubmitListItem *item = [[QuizSubmitListItem alloc] init];
        item.name = [NSString stringWithFormat:@"%d学生",i];
        item.time = [NSString stringWithFormat:@"%d:18 17:29",i];
        item.score = [NSString stringWithFormat:@"%d分",i*10];
        item.rank = [NSString stringWithFormat:@"%d",i];
        [self.dataM addObject:item];
    }
    [self addtable];
}

#pragma mark - 配置数据源
- (void)addtable{
    if (!self.tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:tableView];
        self.tableView = tableView;
    }
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.offset(-84);
        make.top.offset(64);
    }];
    
    // 创建headView
    if (!self.headV) {
        QuizHeadView *head = [QuizHeadView headViewWithSubmitType:self.type];
        head.delegate = self;
        self.headV = head;
    }
    CGFloat headH = [self.headV getRealHeight];
    self.headV.height = headH;
    self.tableView.tableHeaderView = self.headV;
}

#pragma mark - QuizAnalyViewDelegate
- (void)analyView:(QuizAnalyView *)analyView didSelectItem:(QuizAnalyListItem *)item atIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"item.name = %@ indexPath = %ld",item.number,indexPath.row);
}

#pragma mark - UITableDelegate / DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"tableContentSize = %@",NSStringFromCGSize(self.tableView.contentSize));
    return self.dataM.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QuizSubmitListCell *cell = [QuizSubmitListCell cellWithTableView:tableView];
    cell.item = [self.dataM objectAtIndexSafe:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

#pragma mark - lazyload
- (NSMutableArray *)dataM{
    if(!_dataM){
        _dataM = [NSMutableArray array];
    }
    return _dataM;
}


@end
